<?php

namespace App\Trip\Train\TrainSchedule;

use Exception;
use Illuminate\Support\Facades\Validator;

class TrainPassPoint
{
    const REQUIRED_FIELDS
        = [
            "train_code"    => "required",
            "sequence"      => "",
            "arrive_time"   => "required",
            "start_time"    => "required",
            "station_code"  => "required",
            "stopover_time" => "",
            "state"         => "sometimes|required|in:0,1",
        ];
    private string $train_code;
    private string $station_code;
    private int    $sequence;
    private int    $stopover_time;
    private string $arrive_time;
    private string $start_time;
    private int    $state;

    /**
     * @throws Exception
     */
    public function __construct(array $pointData)
    {
        $validator = Validator::make($pointData, self::REQUIRED_FIELDS);
        if ($validator->fails()) {
            throw new Exception("参数错误");
        }
        $this->state = 0;
        foreach ($pointData as $key => $data) {
            $this->$key = $data;
        }
    }

    public function __get(string $name)
    {
        return $this->$name;
    }

    public function __set($name, $value)
    {
        $this->$name = $value;
    }

    public function __toString()
    {
        return json_encode($this->toArray(), true);
    }

    public function toArray(): array
    {
        $arr = [];
        foreach (self::REQUIRED_FIELDS as $field => $unused) {
            $arr[$field] = $this->$field;
        }
        return $arr;
    }

    /**
     * 获取车次号
     * @return string
     */
    public function getTrainCode(): string
    {
        return $this->train_code;
    }

    /**
     * 获取站点编码
     * @return string
     */
    public function getStationCode(): string
    {
        return $this->station_code;
    }

    /**
     * 获取站序
     * @return int
     */
    public function getSequence(): int
    {
        return $this->sequence;
    }

    public function getState(): int
    {
        return $this->state;
    }

    /**
     * 获取停留时间
     * @return int
     */
    public function getStopoverTime(): int
    {
        return $this->stopover_time;
    }

    /**
     * 获取结束时间
     * @return string
     */
    public function getArriveTime(): string
    {
        return $this->arrive_time;
    }

    /**
     * 获取开始时间
     * @return string
     */
    public function getStartTime(): string
    {
        return $this->start_time;
    }
}